package hxy.dragon.ktor.router

import hxy.dragon.ktor.data.BaseResponse
import hxy.dragon.ktor.model.ExposedColleagues
import hxy.dragon.ktor.service.ColleagueService
import io.ktor.http.HttpStatusCode
import io.ktor.server.application.Application
import io.ktor.server.application.log
import io.ktor.server.request.receive
import io.ktor.server.response.respond
import io.ktor.server.routing.delete
import io.ktor.server.routing.get
import io.ktor.server.routing.post
import io.ktor.server.routing.put
import io.ktor.server.routing.route
import io.ktor.server.routing.routing

/**
 * @description
 * @author HOX4SGH
 * @date 2024/7/13
 */

fun Application.configureColleagueRouter() {

    routing {
//        https://ktor.io/docs/server-routing.html#group_by_path
        route("/colleague") {

            post("/create") {
                val colleague = call.receive<ExposedColleagues>()
                var id = ColleagueService.create(colleague)
                log.info("接收参数 $colleague 插入id $id")
                call.respond(HttpStatusCode.Created, id)
            }

            get("/list") {
                var colleagues = ColleagueService.listColleague()
                if (colleagues != null && colleagues.isNotEmpty()) {
                    call.respond(HttpStatusCode.OK, BaseResponse(200, "数据库记录存在", colleagues))
                } else {
                    log.warn("重启后先插入数据库， 数据库为空$colleagues")
                    call.respond(HttpStatusCode.NotFound, BaseResponse(404, "数据库暂无记录", ""))
                }
            }

            get("/{id}") {
                var id = call.parameters["id"]?.toInt() ?: throw IllegalArgumentException("Invalid ID")
                ColleagueService.selectById(id)?.let {
                    call.respond(HttpStatusCode.OK, BaseResponse(200, "数据库记录存在", it))
                } ?: run {
                    log.warn("数据库为空,id= $id")
                    call.respond(HttpStatusCode.NotFound, BaseResponse(404, "数据库暂无id为${id}的记录", ""))
                }
            }

            put("/update") {
                val colleague = call.receive<ExposedColleagues>()
                ColleagueService.updateColleague(colleague)?.let {
                    call.respond(HttpStatusCode.OK, BaseResponse(200, "数据库记录更新成功", it))
                } ?: run {
                    call.respond(
                        HttpStatusCode.NotFound,
                        BaseResponse(404, "Colleague not found or could not be updated", "")
                    )
                }
            }

            delete("/{id}") {
                var id = call.parameters["id"]?.toInt() ?: throw IllegalArgumentException("Invalid ID")
                val deleteColleague = ColleagueService.deleteColleague(
                    id
                )
                if (deleteColleague) {
                    call.respond(HttpStatusCode.OK, BaseResponse(200, "$id deleted", ""))
                } else {
                    call.respond(
                        HttpStatusCode.NotFound,
                        BaseResponse(404, "Colleague not found or could not be deleted", "")
                    )
                }
            }
        }
    }

}